package ai.xiaodao.config;


import ai.xiaodao.tool.ConfigUtil;
import com.futu.openapi.pb.TrdCommon;
import org.apache.log4j.Logger;

import java.util.Properties;

import static java.lang.System.exit;

public class BrokerConfig {
    private static Properties prop = new Properties();

    static Logger log = Logger.getLogger(BrokerConfig.class.getName());

    /*
    初始化券商相关配置，如果初始化失败，则直接退出
     */
    public static void getIns(String conf) {
        //check the input config file path
        if (null != conf && conf.length() > 0) {
            prop = ConfigUtil.readProps(conf);
        } else {
            log.error("The config path in correct please check");
            exit(0);
        }
    }

    public static String getProp(String key) {
        return prop.getProperty(key);
    }

    public static Properties getProps() {
        return prop;
    }

    public static Long getUserID() {
        return Long.valueOf(prop.getProperty(Const.USER_ID_KEY));

    }


    public static Long getTrdAcc() {
        return Long.valueOf(prop.getProperty(Const.USER_TRACE_ACC_KEY));
    }

    public static String getUnlockTradePwdMd5() {
        return prop.getProperty(Const.UNLOCK_TRADEPWD_MD5_KEY);
    }

    public static String getOpendIP() {
        return prop.getProperty(Const.OPEND_IP_KEY);
    }

    public static Short getOpendPort() {
        return Short.valueOf(prop.getProperty(Const.OPEND_PORT_key));
    }

    public static String getRsaKeyFilePath() {
        return prop.getProperty(Const.OPEND_PORT_key);
    }

    public static int getTrdEnv() {
        return parseTrdEnv(prop.getProperty(Const.TRD_ENV));
    }

    /**
     * parse the program running mode
     * "test" mean in test and value 0
     * "production" mean real trade and value 1
     *
     * @param key
     * @return
     */

    static int parseTrdEnv(String key) {
        switch (key) {
            case "simulate":
                return TrdCommon.TrdEnv.TrdEnv_Simulate_VALUE;
            case "real":
                return TrdCommon.TrdEnv.TrdEnv_Real_VALUE;
        }
        return 0;
    }

}
